//
//  RegistController.m
//  MyAppTest
//
//  Created by Sunshine on 2018/7/11.
//  Copyright © 2018年 Sunshine. All rights reserved.
//

#import "RegistController.h"
#import "ShengMingCon.h"
@interface RegistController ()<UITextFieldDelegate>

@property(nonatomic,strong)NSString *yzmStr;
@property(nonatomic,strong)UIView *smBackview;
@property(nonatomic,strong)NSString *smContentstr;
@property(nonatomic,strong)MineButton *selebtn;

@end

@implementation RegistController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/statement/app/list.do"];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
    [self ShengMingMethodWithUrl:endstr];
    self.sexStr = @"";
    [self creatHeaderViewMethod];
}

-(void)ShengMingMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSArray *arr = response;
        if (arr.count > 0) {
            NSDictionary *dic = arr[0];
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                self.smContentstr = dic[@"CONTENT"];
            }];
        }
        
    } failBlock:^(NSError *error) {
        
    }];
    
}

-(void)creatHeaderViewMethod{
    self.view.backgroundColor = LineBackColor;
    UIView *backview = [UIView new];
    backview.backgroundColor = [UIColor whiteColor];
    backview.layer.masksToBounds = YES;
    backview.layer.cornerRadius = 5*kJLXWidthScale;
    [self.view addSubview:backview];
    backview.sd_layout
    .topSpaceToView(self.view, 20*kJLXWidthScale)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .rightSpaceToView(self.view, 20*kJLXWidthScale)
    .heightIs(250*kJLXWidthScale);
    for (int i = 0; i < 5; i ++) {
        UITextField *field = [UITextField new];
        field.delegate = self;
        field.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
        [backview addSubview:field];
        field.sd_layout
        .topSpaceToView(backview, i*50*kJLXWidthScale)
        .rightSpaceToView(backview, 10*kJLXWidthScale)
        .leftSpaceToView(backview, 10*kJLXWidthScale)
        .heightIs(50*kJLXWidthScale);
        if (i == 0) {
            field.placeholder = @"手机号";
            self.phfield = field;
        }else if(i == 1){
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor lightGrayColor];
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 10*kJLXWidthScale;
            
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(yzmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [backview addSubview:btn];
            btn.sd_layout
            .topSpaceToView(backview, i*55*kJLXWidthScale)
            .heightIs(40*kJLXWidthScale)
            .rightSpaceToView(backview, 20*kJLXWidthScale)
            .widthIs(110*kJLXWidthScale);
            field.placeholder = @"输入验证码";
            field.sd_layout
            .topSpaceToView(backview, 50*kJLXWidthScale)
            .heightIs(50*kJLXWidthScale)
            .rightSpaceToView(btn, 10*kJLXWidthScale)
            .leftSpaceToView(backview, 10*kJLXWidthScale);
            self.yzmfield = field;
        }else if (i == 3){
            field.secureTextEntry = YES;
            field.placeholder = @"密码";
            self.psfield = field;
        }else if (i == 4){
            field.secureTextEntry = YES;
            field.placeholder = @"确认密码";
            self.cfpsfield = field;
        }else if(i == 2){
            field.hidden = YES;
            UILabel *lab = [UILabel new];
            lab.text = @"性别(选填)：";
            lab.textColor = [UIColor lightGrayColor];
            [backview addSubview:lab];
            lab.sd_layout
            .topSpaceToView(backview, i*50*kJLXWidthScale)
            .leftSpaceToView(backview, 10*kJLXWidthScale)
            .heightIs(50*kJLXWidthScale)
            .widthIs(120*kJLXWidthScale);
            for (int j = 0; j < 2; j ++) {
                MineButton *btn = [MineButton buttonWithType:UIButtonTypeCustom];
                btn.tag = 600 + j;
                btn.frame = CGRectMake(120*kJLXWidthScale + (60*kJLXWidthScale + 20*kJLXWidthScale)*j, i*50, 60*kJLXWidthScale, 50*kJLXWidthScale);
                [btn addTarget:self action:@selector(boyAndGirlButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [backview addSubview:btn];
                btn.mineimagevi = [UIImageView new];
                btn.mineimagevi.userInteractionEnabled = NO;
                btn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
                [btn addSubview:btn.mineimagevi];
                btn.mineimagevi.sd_layout
                .centerYEqualToView(btn)
                .leftSpaceToView(btn, 0)
                .heightIs(20*kJLXWidthScale)
                .widthIs(20*kJLXWidthScale);
                
                btn.minlab = [UILabel new];
                btn.minlab.userInteractionEnabled = NO;
                btn.minlab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
                [btn addSubview:btn.minlab];
                btn.minlab.textColor = [UIColor darkGrayColor];
                if (j == 0) {
                    btn.minlab.text = @"男";
                }else{
                    btn.minlab.text = @"女";
                }
                btn.minlab.sd_layout
                .centerYEqualToView(btn)
                .leftSpaceToView(btn.mineimagevi, 5*kJLXWidthScale)
                .heightIs(35*kJLXWidthScale)
                .widthIs(35*kJLXWidthScale);
                if (j == 0) {
//                    btn.mineimagevi.image = [UIImage imageNamed:@"LoginSelect"];
//                    btn.minlab.textColor = [UIColor orangeColor];
//                    self.sexStr = @"male";
                }
            }
            
        }
        
        
    }
    
    
    MineButton *btn = [MineButton buttonWithType:UIButtonTypeCustom];
    self.selebtn = btn;
    [btn addTarget:self action:@selector(shengmingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    btn.sd_layout
    .topSpaceToView(backview, 15*kJLXWidthScale)
    .leftSpaceToView(self.view, 15*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale)
    .widthIs(150*kJLXWidthScale);
    
    btn.mineimagevi = [UIImageView new];
    btn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
    [btn addSubview:btn.mineimagevi];
    btn.mineimagevi.sd_layout
    .centerYEqualToView(btn)
    .leftSpaceToView(btn, 0)
    .widthIs(20*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale);
    
    btn.minlab = [UILabel new];
    btn.minlab.font = [UIFont systemFontOfSize:13*kJLXWidthScale];
    btn.minlab.textColor = [UIColor lightGrayColor];
    btn.minlab.text = @"我已阅读并同意";
    [btn addSubview:btn.minlab];
    btn.minlab.sd_layout
    .centerYEqualToView(btn)
    .leftSpaceToView(btn.mineimagevi, 5*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale)
    .rightSpaceToView(btn, 5*kJLXWidthScale);
    
    UIButton *xyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [xyBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    xyBtn.titleLabel.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    [xyBtn setTitle:@"《用户注册协议》" forState:UIControlStateNormal];
    [xyBtn addTarget:self action:@selector(xyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xyBtn];
    xyBtn.sd_layout
    .centerYEqualToView(btn)
    .leftSpaceToView(btn, 0)
    .heightIs(20*kJLXWidthScale)
    .widthIs(150*kJLXWidthScale);
    
    
    for (int i = 0; i < 2; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        if (i == 0) {
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 8*kJLXWidthScale;
            btn.backgroundColor = [UIColor orangeColor];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setTitle:@"注 册" forState:UIControlStateNormal];
        }else{
            [btn setTitle:@"登 录" forState:UIControlStateNormal];
            [btn setTitleColor:BackColor forState:UIControlStateNormal];
        }
        btn.tag = 500 + i;
        [btn addTarget:self action:@selector(registerAndLogButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
        [self.view addSubview:btn];
        btn.sd_layout
        .topSpaceToView(backview,80*kJLXWidthScale + 45*kJLXWidthScale*i + 15*kJLXWidthScale*i )
        .centerXEqualToView(self.view)
        .heightIs(45*kJLXWidthScale)
        .leftSpaceToView(self.view, 20*kJLXWidthScale)
        .rightSpaceToView(self.view, 20*kJLXWidthScale);
    }
}

-(void)shengmingButtonAction:(MineButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        sender.mineimagevi.image = [UIImage imageNamed:@"LoginSelect"];
    }else{
        sender.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
    }
}

-(void)xyButtonAction:(UIButton *)sender{
    self.smBackview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.smBackview.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.5];
    [self.view.window addSubview:self.smBackview];
    
    ShengMingCon *vc = [[ShengMingCon alloc]init];
    vc.contentstr = self.smContentstr;
    [self.smBackview addSubview:vc.view];
    [self addChildViewController:vc];
    vc.view.sd_layout
    .centerYEqualToView(self.smBackview)
    .centerXEqualToView(self.smBackview)
    .widthIs(kScreenWidth - 100*kJLXWidthScale)
    .heightIs(kScreenHeight - 160*kJLXWidthScale);
}

-(void)yzmButtonAction:(UIButton *)sender{
    
    if (self.phfield.text.length == 0) {
        [self alerWithMessage:@"手机号不能为空"];
    }else{
        if(![MethodClass utilityCheckPhoneNumInput:self.phfield.text]) {
            [self alerWithMessage:@"输入正确的手机号"];
            self.phfield.text = @"";
        }else{
            NSString *urlstr = [NSString stringWithFormat:@"/dashop/sms/app/sendsms.do?PHONE=%@",self.phfield.text];
            NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
            [self YZMMethodWithUrl:endstr];
            NSInteger num = 60;
            [MethodClass verificationCode:num blockYes:^{
                [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
            } blockNo:^(id time) {
                [sender setTitle:time forState:UIControlStateNormal];
            }];
          
        }
    }
    
   
    
}

-(void)YZMMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        self.yzmStr = dic[@"STATUSCODE"];
    } failBlock:^(NSError *error) {
        
    }];
}

-(void)boyAndGirlButtonAction:(MineButton *)sender{
    [self.yzmfield endEditing:YES];
    MineButton *boybtn = [self.view viewWithTag:600];
    MineButton *girkbtn = [self.view viewWithTag:601];
    if (sender == boybtn) {
        boybtn.mineimagevi.image = [UIImage imageNamed:@"LoginSelect"];
        boybtn.minlab.textColor = [UIColor orangeColor];
        girkbtn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
        girkbtn.minlab.textColor = [UIColor darkGrayColor];
        self.sexStr = @"male";
    }else{
        girkbtn.mineimagevi.image = [UIImage imageNamed:@"LoginSelect"];
        girkbtn.minlab.textColor = [UIColor orangeColor];
        boybtn.mineimagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
        boybtn.minlab.textColor = [UIColor darkGrayColor];
        self.sexStr = @"female";
    }
}






-(void)registerAndLogButtonAction:(UIButton *)sender{
    [self.view endEditing:YES];
    if (sender.tag - 500 == 0) {
        if (self.phfield.text.length == 0) {
            [self alerWithMessage:@"输入正确的手机号"];
        }
//        else if (self.sexStr.length == 0){
//            [self alerWithMessage:@"选择性别"];
//        }
        else if (self.psfield.text.length == 0){
            [self alerWithMessage:@"输入密码"];
        }else if(self.yzmfield.text.length == 0){
            [self alerWithMessage:@"验证码不能为空"];
        }else if(self.cfpsfield.text == 0){
            [self alerWithMessage:@"确认密码不能为空"];
        }else{
            /*
             接口传参 PHONE 手机号
             SEX 性别
             PASSWORD 密码
             PASSWORD1 确认密码
             否需要分页 否
             接口地址 happuser/app/regist.do
             */
            if (self.selebtn.selected) {
                NSString *strurl = [NSString stringWithFormat:@"/dashop/happuser/app/regist.do?PHONE=%@&SEX=%@&PASSWORD=%@&PASSWORD1=%@",self.phfield.text,self.sexStr,self.psfield.text,self.cfpsfield.text];
                NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
                NSString  *newUrlString = [endstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
                [self RegistMethodWithUrl:newUrlString];
            }else{
                [self alerWithMessage:@"请阅读协议"];
            }
        }
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)RegistMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                self.reguistBlock(self.phfield.text, self.psfield.text);
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [self alerWithMessage:dic[@"MESSAGE"]];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}



//已经取消编辑状态 结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.placeholder isEqualToString:@"输入验证码"]) {
        if (![textField.text isEqualToString:self.yzmStr]) {
            [self alerWithMessage:@"验证码不正确"];
            textField.text = @"";
        }
    }else if ([textField.placeholder isEqualToString:@"确认密码"]){
        if (![textField.text isEqualToString:self.psfield.text]) {
            [self alerWithMessage:@"密码不一致"];
            textField.text = @"";
        }
       
    }
    
}


//封装提示框方法
-(void)alerWithMessage:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延迟1秒自动消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

@end
